﻿Imports VN.INS.BL.BC
Imports System.Data
Imports System.IO
Imports Microsoft.Office.Interop

Partial Class Devida_ReporteFormato5
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not (Me.Page.IsPostBack) Then
            ListaPPER()
            Listaperiodo()
            btnGenerar.Attributes.Add("onclick", "javascript:document.getElementById('" + btnGenerar.ClientID + "').disabled=true;" + Me.GetPostBackEventReference(btnGenerar))
        End If
    End Sub

    Protected Sub gvTipo_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvTipo.RowDataBound
        Dim TotalProducto As Double = 0
        Dim ProgramadoProducto As Double = 0
        Dim TransferidoProducto As Double = 0
        Dim EjacutadoProducto As Double = 0
        Dim PorcProducto As Double = 0
        Dim IAFG_Productos As Double = 0
        Dim vst As DataView
        Dim dt As DataTable
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim hfTipo As HiddenField = CType(e.Row.FindControl("hfTipo"), HiddenField)
            Dim lblTipo As Label = CType(e.Row.FindControl("lblTipo"), Label)
            ViewState("Tipo") = lblTipo.Text
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
            Dim lblTotal As Label = CType(e.Row.FindControl("lblTotal"), Label)
            Dim lblTotalProducto As Label = CType(e.Row.FindControl("lblTotalProducto"), Label)
            Dim lblProgramadoProducto As Label = CType(e.Row.FindControl("lblProgramadoProducto"), Label)
            Dim lblTransferidoProducto As Label = CType(e.Row.FindControl("lblTransferidoProducto"), Label)
            Dim lblEjacutadoProducto As Label = CType(e.Row.FindControl("lblEjacutadoProducto"), Label)
            Dim lblPorcProducto As Label = CType(e.Row.FindControl("lblPorcProducto"), Label)
            Dim lblIAFGProductos As Label = CType(e.Row.FindControl("lblIAFGProductos"), Label)
            lblTotal.Text = "Total " + ViewState("Tipo")
            vst = New DataView(ViewState("Data"), "ID_TIPO='" + hfTipo.Value.ToString + "'", "", DataViewRowState.CurrentRows)
            dt = New DataTable
            dt = vst.ToTable
            For i As Int16 = 0 To dt.Rows.Count - 1
                TotalProducto = TotalProducto + Convert.ToDouble(IIf(dt.Rows(i).Item("TOTAL").ToString = "", 0, dt.Rows(i).Item("TOTAL").ToString))
                ProgramadoProducto = ProgramadoProducto + Convert.ToDouble(IIf(dt.Rows(i).Item("PROG_ACUMULADO").ToString = "", 0, dt.Rows(i).Item("PROG_ACUMULADO").ToString))
                TransferidoProducto = TransferidoProducto + Convert.ToDouble(IIf(dt.Rows(i).Item("MONTO_TRANSFERIDO").ToString = "", 0, dt.Rows(i).Item("MONTO_TRANSFERIDO").ToString))
                EjacutadoProducto = EjacutadoProducto + Convert.ToDouble(IIf(dt.Rows(i).Item("EJECUTADO").ToString = "", 0, dt.Rows(i).Item("EJECUTADO").ToString))
                IAFG_Productos = IAFG_Productos + Convert.ToDouble(IIf(dt.Rows(i).Item("IAFG").ToString = "", 0, dt.Rows(i).Item("IAFG").ToString))
            Next
            PorcProducto = IIf(ProgramadoProducto = 0, 0, EjacutadoProducto / ProgramadoProducto)
            lblTotalProducto.Text = TotalProducto.ToString("##,#0.00")
            lblProgramadoProducto.Text = ProgramadoProducto.ToString("##,#0.00")
            lblTransferidoProducto.Text = TransferidoProducto.ToString("##,#0.00")
            lblEjacutadoProducto.Text = EjacutadoProducto.ToString("##,#0.00")
            lblPorcProducto.Text = PorcProducto.ToString("#,##0 %")            
            lblIAFGProductos.Text = (IAFG_Productos / dt.Rows.Count).ToString("#,##0 %")
            Dim gdv As GridView = e.Row.Cells(1).FindControl("gvDetalle")
            gdv.DataSource = vst
            gdv.DataBind()
            vst = Nothing
            dt.Dispose()
        End If
    End Sub

    Public Sub RDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then            
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
            Dim _Row As System.Data.DataRowView = e.Row.DataItem
            Select Case _Row.Item(0).ToString
                Case "PPE-0001"
                    Dim PresMan As Decimal = Convert.ToDecimal(_Row.Item(2).ToString)
                    Dim MonGir As Decimal = Convert.ToDecimal(_Row.Item(3).ToString)
                    Dim price1 As Decimal = Convert.ToDecimal(_Row.Item(6).ToString)
                    Dim AvanGir As Decimal = Convert.ToDecimal(_Row.Item(7).ToString)

                Case "PPE-0002"
                    Dim PresMan As Decimal = Convert.ToDecimal(_Row.Item(2).ToString)
                    Dim MonGir As Decimal = Convert.ToDecimal(_Row.Item(3).ToString)
                    Dim price2 As Decimal = Convert.ToDecimal(_Row.Item(6).ToString)
                    Dim AvanGir As Decimal = Convert.ToDecimal(_Row.Item(7).ToString)

                Case "PPE-0003"
                    Dim PresMan As Decimal = Convert.ToDecimal(_Row.Item(2).ToString)
                    Dim MonGir As Decimal = Convert.ToDecimal(_Row.Item(3).ToString)
                    Dim price3 As Decimal = Convert.ToDecimal(_Row.Item(6).ToString)
                    Dim AvanGir As Decimal = Convert.ToDecimal(_Row.Item(7).ToString)

            End Select        
        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)

            e.Row.Cells(0).ColumnSpan = 2
            e.Row.Cells(0).Text = "Totales"
            e.Row.Font.Bold = True
            e.Row.ForeColor = Drawing.Color.Black
            e.Row.HorizontalAlign = HorizontalAlign.Right
            e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
            e.Row.Cells(4).Text = "1.00"            
        End If
    End Sub

    Private Sub Listaperiodo()
        Dim objBC As GeneralBC
        Dim objDt As DataTable
        Try
            objBC = New GeneralBC
            objDt = New DataTable
            objDt = objBC.ListaMes(Session("Año"))
            ddlPeriodo.DataSource = objDt
            ddlPeriodo.DataTextField = "DESCRIPCION"
            ddlPeriodo.DataValueField = "PERIODO"
            ddlPeriodo.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub ListaPPER()
        Dim objPPER As PPERBC
        Dim objData As DataTable
        objPPER = New PPERBC
        objData = New DataTable
        objData = objPPER.ListaPPER("")
        ddlPPER.DataSource = objData
        ddlPPER.DataTextField = "DESCRIPCION"
        ddlPPER.DataValueField = "PPER"
        ddlPPER.DataBind()
    End Sub

    Protected Sub btnGenerar_Click(sender As Object, e As ImageClickEventArgs) Handles btnGenerar.Click

        divReporte.Style("display") = "none"

        Dim objBC As New FormatoBC
        Dim objDt As New DataTable
        Dim objDt1 As New DataTable
        Dim objDt2 As New DataTable

        Dim TotalPPER As Double = 0
        Dim ProgramadoPPER As Double = 0
        Dim TransferidoPPER As Double = 0
        Dim EjecutadoPPER As Double = 0
        Dim PorcPPER As Double = 0
        Dim IAFG_PPER As Double = 0

        Dim TotalProductos As Double = 0
        Dim ProgramadoProductos As Double = 0
        Dim TransferidoProductos As Double = 0
        Dim EjecutadoProductos As Double = 0
        Dim PorcProductos As Double = 0
        Dim IAFG_Productos As Double = 0

        Dim TotalProyectos As Double = 0
        Dim ProgramadoProyectos As Double = 0
        Dim TransferidoProyectos As Double = 0
        Dim EjecutadoProyectos As Double = 0
        Dim PorcProyectos As Double = 0
        Dim IAFG_Proyectos As Double = 0

        Dim TotalAC As Double = 0
        Dim ProgramadoAC As Double = 0
        Dim TransferidoAC As Double = 0
        Dim EjecutadoAC As Double = 0
        Dim PorcAC As Double = 0

        objDt1 = objBC.DetalleFormato5(ddlPPER.SelectedValue, ddlPeriodo.SelectedValue, Session("Año"))
        ViewState("Data") = objDt1
        objDt = objBC.CabeceraFormato5(ddlPPER.SelectedValue, ddlPeriodo.SelectedValue, Session("Año"))



        Dim vst0 As DataView = New DataView(ViewState("Data"), "TIPO_PROYECTO_ACTIVIDAD IN (1,2,3) ", "", DataViewRowState.CurrentRows)
        objDt2 = vst0.ToTable
        For i As Integer = 0 To objDt2.Rows.Count - 1
            TotalPPER = TotalPPER + Convert.ToDouble(IIf(objDt2.Rows(i).Item("TOTAL").ToString = "", "0", objDt2.Rows(i).Item("TOTAL").ToString))
            ProgramadoPPER = ProgramadoPPER + Convert.ToDouble(IIf(objDt2.Rows(i).Item("PROG_ACUMULADO").ToString = "", "0", objDt2.Rows(i).Item("PROG_ACUMULADO").ToString))
            TransferidoPPER = TransferidoPPER + Convert.ToDouble(IIf(objDt2.Rows(i).Item("MONTO_TRANSFERIDO").ToString = "", "0", objDt2.Rows(i).Item("MONTO_TRANSFERIDO").ToString))
            EjecutadoPPER = EjecutadoPPER + Convert.ToDouble(IIf(objDt2.Rows(i).Item("EJECUTADO").ToString = "", "0", objDt2.Rows(i).Item("EJECUTADO").ToString))
            IAFG_PPER = IAFG_PPER + Convert.ToDouble(IIf(objDt2.Rows(i).Item("IAFG").ToString = "", "0", objDt2.Rows(i).Item("IAFG").ToString))
        Next
        vst0.Dispose()
        PorcPPER = IIf(ProgramadoPPER = 0, 0, EjecutadoPPER / ProgramadoPPER)
        lblTotalPPER.Text = TotalPPER.ToString("##,###.00")
        lblProgramadoPPER.Text = ProgramadoPPER.ToString("##,###.00")
        lblTransferidoPPERs.Text = TransferidoPPER.ToString("##,##0.00")
        lblEjecutadoPPER.Text = EjecutadoPPER.ToString("##,##0.00")
        lblPorcPPER.Text = PorcProductos.ToString("#,##0 %")
        lblIAFG_PPER.Text = (IAFG_PPER / objDt2.Rows.Count).ToString("#,##0 %")

        Dim vst As DataView = New DataView(ViewState("Data"), "TIPO_PROYECTO_ACTIVIDAD = 2 ", "", DataViewRowState.CurrentRows)
        objDt2 = vst.ToTable
        For i As Integer = 0 To objDt2.Rows.Count - 1
            TotalProductos = TotalProductos + Convert.ToDouble(IIf(objDt2.Rows(i).Item("TOTAL").ToString = "", "0", objDt2.Rows(i).Item("TOTAL").ToString))
            ProgramadoProductos = ProgramadoProductos + Convert.ToDouble(IIf(objDt2.Rows(i).Item("PROG_ACUMULADO").ToString = "", "0", objDt2.Rows(i).Item("PROG_ACUMULADO").ToString))
            TransferidoProductos = TransferidoProductos + Convert.ToDouble(IIf(objDt2.Rows(i).Item("MONTO_TRANSFERIDO").ToString = "", "0", objDt2.Rows(i).Item("MONTO_TRANSFERIDO").ToString))
            EjecutadoProductos = EjecutadoProductos + Convert.ToDouble(IIf(objDt2.Rows(i).Item("EJECUTADO").ToString = "", "0", objDt2.Rows(i).Item("EJECUTADO").ToString))
            IAFG_Productos = IAFG_Productos + Convert.ToDouble(IIf(objDt2.Rows(i).Item("IAFG").ToString = "", "0", objDt2.Rows(i).Item("IAFG").ToString))
        Next
        vst.Dispose()
        PorcProductos = IIf(ProgramadoProductos = 0, 0, EjecutadoProductos / ProgramadoProductos)
        lblTotalProductos.Text = TotalProductos.ToString("##,###.00")
        lblProgramadoProductos.Text = ProgramadoProductos.ToString("##,###.00")
        lblTransferidoProductos.Text = TransferidoProductos.ToString("##,##0.00")
        lblEjecutadoProductos.Text = EjecutadoProductos.ToString("##,##0.00")
        lblPorcProductos.Text = PorcProductos.ToString("#,##0 %")
        lblIAFG_Productos.Text = (IAFG_Productos / objDt2.Rows.Count).ToString("#,##0 %")

        Dim vst1 As DataView = New DataView(ViewState("Data"), "TIPO_PROYECTO_ACTIVIDAD = 1 ", "", DataViewRowState.CurrentRows)
        objDt2 = vst1.ToTable
        For i As Integer = 0 To objDt2.Rows.Count - 1
            TotalProyectos = TotalProyectos + Convert.ToDouble(IIf(objDt2.Rows(i).Item("TOTAL").ToString = "", "0", objDt2.Rows(i).Item("TOTAL").ToString))
            ProgramadoProyectos = ProgramadoProyectos + Convert.ToDouble(IIf(objDt2.Rows(i).Item("PROG_ACUMULADO").ToString = "", "0", objDt2.Rows(i).Item("PROG_ACUMULADO").ToString))
            TransferidoProyectos = TransferidoProyectos + Convert.ToDouble(IIf(objDt2.Rows(i).Item("MONTO_TRANSFERIDO").ToString = "", "0", objDt2.Rows(i).Item("MONTO_TRANSFERIDO").ToString))
            EjecutadoProyectos = EjecutadoProyectos + Convert.ToDouble(IIf(objDt2.Rows(i).Item("EJECUTADO").ToString = "", "0", objDt2.Rows(i).Item("EJECUTADO").ToString))
            IAFG_Proyectos = IAFG_Proyectos + Convert.ToDouble(IIf(objDt2.Rows(i).Item("IAFG").ToString = "", "0", objDt2.Rows(i).Item("IAFG").ToString))
        Next
        vst1.Dispose()
        PorcProyectos = IIf(ProgramadoProyectos = 0, 0, EjecutadoProyectos / ProgramadoProyectos)
        lblTotalProyectos.Text = TotalProyectos.ToString("##,###.00")
        lblProgramadoProyectos.Text = ProgramadoProyectos.ToString("##,###.00")
        lblTransferidoProyectos.Text = TransferidoProyectos.ToString("##,##0.00")
        lblEjecutadoProyectos.Text = EjecutadoProyectos.ToString("##,##0.00")
        lblPorcProyectos.Text = PorcProyectos.ToString("#,##0 %")
        lblIAFG_Proyectos.Text = (IAFG_Proyectos / objDt2.Rows.Count).ToString("#,##0 %")

        ViewState("dtTipo") = objDt
        gvTipo.DataSource = objDt
        gvTipo.DataBind()

        divReporte.Style("display") = "block"

    End Sub

End Class
